package cn.lee.chp2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Locale;

import javax.servlet.ServletOutputStream;
import javax.servlet.ServletResponse;

import cn.lee.TomcatConstants;

/**
 * HTTP response - status-Line
 * ((general-header|response-header|entity-header)CRLF)
 * CRLF
 * [message-body]
 * status-line=HTTP-Version SP Status-Code SP Reason-Phrase CRLF
 * @author huaigang
 *
 */
public class Response implements ServletResponse{
    OutputStream output;
    Request request;
    public static final int BUFFER_SIZE = 2048;
    public Response(){
        
    }
    public Response(OutputStream output) {
        this.output = output;
    }
    
    public void setRequest(Request request){
        this.request = request;
    }
    public void sendStaticResource() throws IOException{
        String uri = request.getUri();
        File file = new File(TomcatConstants.WEB_ROOT, uri);
        if(!file.exists()){
            file = new File(TomcatConstants.WEB_ROOT, TomcatConstants.URL_404);
        }
        InputStream is = null;
        try{
            is = new FileInputStream(file);
            byte[] b = new byte[BUFFER_SIZE];
            int rsize = 0;
            while((rsize = is.read(b)) != -1){
                output.write(b, 0, rsize);
            }
        }finally{
            is.close();
        }
    }

    @Override
    public void flushBuffer() throws IOException {
        // TODO Auto-generated method stub
        
    }

    @Override
    public int getBufferSize() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public String getCharacterEncoding() {
        // TODO Auto-generated method stub
        return null;
    }

    public String getContentType() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Locale getLocale() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        return null;
    }

    @Override
    public PrintWriter getWriter() throws IOException {
        PrintWriter out = new PrintWriter(output, true);
        return out;
    }

    @Override
    public boolean isCommitted() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void reset() {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void resetBuffer() {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void setBufferSize(int arg0) {
        // TODO Auto-generated method stub
        
    }

    public void setCharacterEncoding(String arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void setContentLength(int arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void setContentType(String arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void setLocale(Locale arg0) {
        // TODO Auto-generated method stub
        
    }
}
